{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [2.A Bit of Python](02.00-A-Bit-of-Python.ipynb) | [Contents](Index.ipynb) | [Data Structures](02.02-Data-Structures.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 数据类型\n",
    "\n",
    "有两种主要地的数据类型：数字和字符串。类型函数返回数据类型。\n",
    "\n",
    "### 2.1.1 数字\n",
    "\n",
    "在Python中有三种类型的数字：整数、浮点数和复数。整数是数组(向量，矩阵)索引、计数所需要的等。在Python中，不需要事先定义变量类型，在需要的时候，甚至可以在程序的后面修改数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 5\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这意味着，数据类型是整数。程序中没有`>>>`的行表示Python的输出。另一种最常用的数据类型是浮点数。大多数水文变量都属于这一类数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = 7.8\n",
    "type(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这意味着数据类型是浮点数。另一种数据类型是复数，这在日常的水文生活中并不经常需要。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "complex"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = 5 + 2j\n",
    "type(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`c`表示复数数据类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  2.1.2 字符串\n",
    "\n",
    "字符串是字符序列。有三种指定字符串的方式。\n",
    "\n",
    "**单引号:**在Python中写在单引号里面的被称为字符串。\n",
    "\n",
    "**双引号:**双引号也用于定义字符串。如果单引号就能够定义字符串，为什么还需要双引号呢？让我们尝试使用单引号写`‘What's your name?’`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-5-95ba94c3d590>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-5-95ba94c3d590>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    foo = 'what's your name?'\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "foo = 'what's your name?'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里产生了语法错误。让我们尝试使用双引号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what's your name?\n"
     ]
    }
   ],
   "source": [
    "foo = \"what's your name?\"\n",
    "print(foo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "双引号是定义包含单引号字符串的一种简单方式。然而，相同的任务也可以采用单引号做到。相同的字符串只需要在`'`之前添加`\\`就可以使用单引号编写了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what's your name?\n"
     ]
    }
   ],
   "source": [
    "foo = 'what\\'s your name?'\n",
    "print(foo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**三引号:**当字符串空格超过一行时，三引号是定义它们的最好方式。多行字符串可以通过在单引号或双引号中指定换行符`\\n`来实现，而三引号则进行了简化。三引号在其他事上(为函数写帮助内容)也有很大用处，你将在本书后面的内容中读到。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo = \"\"\"My name is Sat Kumar.\n",
    "... I am in PhD \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My name is Sat Kumar.\n",
      "I am in PhD \n"
     ]
    }
   ],
   "source": [
    "print(foo)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
